<?php
/**
 * data controller
 */
namespace App\Http\Controllers;

use App\Service\DataService;
use App\Validation\ListValidation;
use App\Validation\DeleteValidation;

class DataController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const COLLECT_TRUE = 1;		//收藏标识：是
	const COLLECT_FALSE = 0;	//收藏标识：否
	const PAY_TRUE = 1;			//支付标识：是
	const PAY_FALSE = 0;		//支付标识：否

	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser(0);

		$raw = $this->validation(new ListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$data = DataService::getList($raw);

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->data . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
			$d[$key]->is_collect = intval($value->is_collect) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_pay = intval($value->is_pay) ? (string)self::PAY_TRUE : (string)self::PAY_FALSE;
			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$key]->pay_num = $value->pay_num > 10000 ? floor($value->pay_num / 10000) . '万+' : $value->pay_num;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
				$value->tags = '';

			$d[$key]->create_time = $this->getShowTime($value->create_time);
			$d[$key]->tags = $value->tags ? $value->tags : "";
		}

		$count = DataService::getList($raw, TRUE);
		$assign = [
			'total' => $count,
			'list'  => $d,
		];

		return $this->echoJson($assign);
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser(0);

		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson();

		if (isset($this->user)){
			$data = DataService::getInfo($id, $this->user->member_id);
		}else{
			$data = DataService::getInfo($id);
		}
		if (is_object($data)){
			//判断缩略图是否有图且url是否完整
			// $data->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$data->pics = explode(',', $data->pics);
			$data->pics = array_filter($data->pics);
		}
		if ($data){
			$data->go_url = $this->config->gourl . $this->config->urls->data . $data->id;
			$data->three_url = $data->three_url ? $data->three_url : $data->go_url;
			$data->content = $data->content ? $data->content : "";
			$data->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$data->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$data->pay_num = $data->pay_num > 10000 ? floor($data->pay_num / 10000) . '万+' : $data->pay_num;
			$data->download = $data->download > 10000 ? floor($data->download / 10000) . '万+' : $data->download;
			$data->share = $data->share > 10000 ? floor($data->share / 10000) . '万+' : $data->share;
			$data->sources = $data->sources ? $data->sources : "觅窝";
		}
		
		return $this->echoJson($data);
	}

	/**
	 * [dwonloadAction 下载量+1]
	 * @return [type] [description]
	 */
	public function downloadAction()
	{
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = DataService::download($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [shareAction 分享量+1]
	 * @return [type] [description]
	 */
	public function shareAction()
	{
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = DataService::share($raw->id);
		return $this->echoJson('ok');
	}
}